package com.module.ble.db

import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：18/7/2025
 *
 * 描述：HeartEntryTimeDao查询辅助类
 *
 * 修订历史：
 *
 */
object HeartEntryTimeQueryHelper {

    private val validFields = setOf(
        "hasHeartRate", "hasBloodOxygen", "hasBodyTemperature",
        "hasEnvTemperature", "hasSleepInfo", "hasStepNumber"
    )

    /**
     * 根据字段名查询该字段为1的所有数据
     * @param fieldName 字段名（如：hasHeartRate, hasBloodOxygen, hasBodyTemperature等）
     * @return SupportSQLiteQuery
     */
    fun createFieldEqualsOneQuery(fieldName: String): SupportSQLiteQuery {
        if (!validFields.contains(fieldName)) {
            throw IllegalArgumentException("Invalid field name: $fieldName")
        }

        val sql = "SELECT * FROM heart_entry_time WHERE $fieldName = 1"
        return SimpleSQLiteQuery(sql)
    }

    /**
     * 根据字段名数组查询任何一个字段为1的所有数据（OR条件）
     * @param fieldNames 字段名数组
     * @return SupportSQLiteQuery
     */
    fun createFieldEqualsOneQuery(fieldNames: Array<String>): SupportSQLiteQuery {
        if (fieldNames.isEmpty()) {
            throw IllegalArgumentException("Field names array cannot be empty")
        }

        // 验证所有字段名都是有效的
        fieldNames.forEach { fieldName ->
            if (!validFields.contains(fieldName)) {
                throw IllegalArgumentException("Invalid field name: $fieldName")
            }
        }

        // 构建OR条件的SQL语句
        val conditions = fieldNames.joinToString(" OR ") { "$it = 1" }
        val sql = "SELECT * FROM heart_entry_time WHERE $conditions"
        return SimpleSQLiteQuery(sql)
    }

    /**
     * 根据字段名数组查询所有字段都为1的数据（AND条件）
     * @param fieldNames 字段名数组
     * @return SupportSQLiteQuery
     */
    fun createAllFieldsEqualsOneQuery(fieldNames: Array<String>): SupportSQLiteQuery {
        if (fieldNames.isEmpty()) {
            throw IllegalArgumentException("Field names array cannot be empty")
        }

        // 验证所有字段名都是有效的
        fieldNames.forEach { fieldName ->
            if (!validFields.contains(fieldName)) {
                throw IllegalArgumentException("Invalid field name: $fieldName")
            }
        }

        // 构建AND条件的SQL语句
        val conditions = fieldNames.joinToString(" AND ") { "$it = 1" }
        val sql = "SELECT * FROM heart_entry_time WHERE $conditions"
        return SimpleSQLiteQuery(sql)
    }
}